﻿using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.Entity.View;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.View
{
    public partial class View_PB_MaterialStockNumberBusiness : BaseBusiness<View_PB_MaterialStockNumber>, IView_PB_MaterialStockNumberBusiness, ITransientDependency
    {
        public View_PB_MaterialStockNumberBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<View_PB_MaterialStockNumber>> GetDataListAsync(PageInput<QueryPBMaterialType> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<View_PB_MaterialStockNumber>();
            var search = input.Search;

            //筛选
            //if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            //{
            //    var newWhere = DynamicExpressionParser.ParseLambda<View_PB_MaterialStockNumber, bool>(
            //        ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
            //    where = where.And(newWhere);
            //}
            q = q.Where(s => s.Id != input.Search.MaterialId);
            if (!search.PBMaterialCode.IsNullOrEmpty())
            {
                q = q.Where(s => s.ProductCode.Contains(input.Search.PBMaterialCode));
            }
            if (!search.PBMaterialName.IsNullOrEmpty())
            {
                q = q.Where(s => s.Name.Contains(input.Search.PBMaterialName) || s.Code.Contains(input.Search.PBMaterialName));
            }
            if (!search.PBMaterialTypeId.IsNullOrEmpty())
            {
                List<string> liststr = new List<string>();
                List<PB_MaterialType> listdata = await Db.GetIQueryable<PB_MaterialType>().ToListAsync();
                var typelist = listdata.Where(x => x.Id == search.PBMaterialTypeId).FirstOrDefault();
                if (typelist!=null)
                {
                    var typelist2 = listdata.Where(x => x.ParentId == typelist.Id).ToList();
                    if (typelist2.Count>0) {
                      
                        foreach (var item in typelist2)
                        {
                            liststr.Add(item.Id);
                            GetItem(item.Id, liststr, listdata);
                        }
                        q = q.Where(w => liststr.Contains(w.MaterialTypeId));
                    }
                    else
                    {
                        q = q.Where(w => w.MaterialTypeId == search.PBMaterialTypeId);
                    }
                }
            }
            return await q.Where(where).GetPageResultAsync(input);
        }

        
        private List<string> GetItem(string Parentid, List<string> liststr, List<PB_MaterialType> listdata)
        {
            var itemlist = listdata.Where(x => x.ParentId == Parentid).ToList();
            foreach (var item in itemlist)
            {
                liststr.Add(item.Id);
                GetItem(item.Id, liststr, listdata);
            }
            return liststr;
        }
        public async Task<View_PB_MaterialStockNumber> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(View_PB_MaterialStockNumber data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(View_PB_MaterialStockNumber data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}